#include "DisplaySettings.h"
#include <QtCore/QSettings>

static const char * const groupKey = "EditorDisplaySettings";
static const char * const keyWrapEdgeColor = "WrapEdgeColor";
static const char * const keyWrapEdgeFillColor = "WrapEdgeFillColor";
static const char * const keyCurrentLineFillColor = "CurrentLineFillColor";
static const char * const keyLineNumbersColor = "LineNumbersColor";
static const char * const keyLineNumbersFillColor = "LineNumbersFillColor";
static const char * const keyFoldingMarkersFillColor = "FoldingMarkersFillColor";
static const char * const keyDisplayLineNumbers = "DisplayLineNumbers";
static const char * const keyTextWrapping = "TextWrapping";
static const char * const keyShowWrapColumn = "ShowWrapColumn";
static const char * const keyWrapColumn = "WrapColumn";
static const char * const keyVisualizeWhitespace = "VisualizeWhitespace";
static const char * const keyDisplayFoldingMarkers = "DisplayFoldingMarkers";
static const char * const keyHighlightCurrentLine = "HighlightCurrentLine";
static const char * const keyHighlightBlocks = "HighlightBlocks";
static const char * const keyAnimateMatchingParentheses = "AnimateMatchingParentheses";
static const char * const keyMarkTextChanges = "MarkTextChanges";
static const char * const keyAutoFoldFirstComment = "AutoFoldFirstComment";
static const char * const keyCenterCursorOnScroll = "CenterCursorOnScroll";

namespace CodeEdit {

DisplaySettings::DisplaySettings()
    // colors
    : wrapEdgeColor("#d1d1d1")
    , wrapEdgeFillColor("#efefef")
    , currentLineFillColor("#b7bac2")
    , lineNumbersColor("#808080")
    , lineNumbersFillColor("#d4d0c8")
    , foldingMarkersFillColor("#ffffff")
    // misc
    , displayLineNumbers(true)
    , textWrapping(false)
    , showWrapColumn(false)
    , wrapColumn(80)
    , visualizeWhitespace(false)
    , displayFoldingMarkers(true)
    , highlightCurrentLine(true)
    , highlightBlocks(false)
    , animateMatchingParentheses(true)
    , markTextChanges(true)
    , autoFoldFirstComment(true)
    , centerCursorOnScroll(false)
{
}

void DisplaySettings::toSettings(QSettings *s) const
{
    s->beginGroup(groupKey);
    // colors
    s->setValue(QLatin1String(keyWrapEdgeColor), wrapEdgeColor.name());
    s->setValue(QLatin1String(keyWrapEdgeFillColor), wrapEdgeFillColor.name());
    s->setValue(QLatin1String(keyCurrentLineFillColor), currentLineFillColor.name());
    s->setValue(QLatin1String(keyLineNumbersColor), lineNumbersColor.name());
    s->setValue(QLatin1String(keyLineNumbersFillColor), lineNumbersFillColor.name());
    s->setValue(QLatin1String(keyFoldingMarkersFillColor), foldingMarkersFillColor.name());
    // misc
    s->setValue(QLatin1String(keyDisplayLineNumbers), displayLineNumbers);
    s->setValue(QLatin1String(keyTextWrapping), textWrapping);
    s->setValue(QLatin1String(keyShowWrapColumn), showWrapColumn);
    s->setValue(QLatin1String(keyWrapColumn), wrapColumn);
    s->setValue(QLatin1String(keyVisualizeWhitespace), visualizeWhitespace);
    s->setValue(QLatin1String(keyDisplayFoldingMarkers), displayFoldingMarkers);
    s->setValue(QLatin1String(keyHighlightCurrentLine), highlightCurrentLine);
    s->setValue(QLatin1String(keyHighlightBlocks), highlightBlocks);
    s->setValue(QLatin1String(keyAnimateMatchingParentheses), animateMatchingParentheses);
    s->setValue(QLatin1String(keyMarkTextChanges), markTextChanges);
    s->setValue(QLatin1String(keyAutoFoldFirstComment), autoFoldFirstComment);
    s->setValue(QLatin1String(keyCenterCursorOnScroll), centerCursorOnScroll);
    s->endGroup();
}

void DisplaySettings::fromSettings(QSettings *s)
{
    *this = DisplaySettings(); // assign defaults

    s->beginGroup(groupKey);
    // colors
    wrapEdgeColor = s->value(QLatin1String(keyWrapEdgeColor), wrapEdgeColor.name()).toString();
    wrapEdgeFillColor = s->value(QLatin1String(keyWrapEdgeFillColor), wrapEdgeFillColor.name()).toString();
    currentLineFillColor = s->value(QLatin1String(keyCurrentLineFillColor), currentLineFillColor.name()).toString();
    lineNumbersColor = s->value(QLatin1String(keyLineNumbersColor), lineNumbersColor.name()).toString();
    lineNumbersFillColor = s->value(QLatin1String(keyLineNumbersFillColor), lineNumbersFillColor.name()).toString();
    foldingMarkersFillColor = s->value(QLatin1String(keyFoldingMarkersFillColor), foldingMarkersFillColor.name()).toString();
    // misc
    displayLineNumbers = s->value(QLatin1String(keyDisplayLineNumbers), displayLineNumbers).toBool();
    textWrapping = s->value(QLatin1String(keyTextWrapping), textWrapping).toBool();
    showWrapColumn = s->value(QLatin1String(keyShowWrapColumn), showWrapColumn).toBool();
    wrapColumn = s->value(QLatin1String(keyWrapColumn), wrapColumn).toInt();
    visualizeWhitespace = s->value(QLatin1String(keyVisualizeWhitespace), visualizeWhitespace).toBool();
    displayFoldingMarkers = s->value(QLatin1String(keyDisplayFoldingMarkers), displayFoldingMarkers).toBool();
    highlightCurrentLine = s->value(QLatin1String(keyHighlightCurrentLine), highlightCurrentLine).toBool();
    highlightBlocks = s->value(QLatin1String(keyHighlightBlocks), highlightBlocks).toBool();
    animateMatchingParentheses = s->value(QLatin1String(keyAnimateMatchingParentheses), animateMatchingParentheses).toBool();
    markTextChanges = s->value(QLatin1String(keyMarkTextChanges), markTextChanges).toBool();
    autoFoldFirstComment = s->value(QLatin1String(keyAutoFoldFirstComment), autoFoldFirstComment).toBool();
    centerCursorOnScroll = s->value(QLatin1String(keyCenterCursorOnScroll), centerCursorOnScroll).toBool();
    s->endGroup();
}

bool DisplaySettings::equals(const DisplaySettings &ds) const
{
    return wrapEdgeColor == ds.wrapEdgeColor
        && wrapEdgeFillColor == ds.wrapEdgeFillColor
        && currentLineFillColor == ds.currentLineFillColor
        && lineNumbersColor == ds.lineNumbersColor
        && lineNumbersFillColor == ds.lineNumbersFillColor
        && foldingMarkersFillColor == ds.foldingMarkersFillColor
        && displayLineNumbers == ds.displayLineNumbers
        && textWrapping == ds.textWrapping
        && showWrapColumn == ds.showWrapColumn
        && wrapColumn == ds.wrapColumn
        && visualizeWhitespace == ds.visualizeWhitespace
        && displayFoldingMarkers == ds.displayFoldingMarkers
        && highlightCurrentLine == ds.highlightCurrentLine
        && highlightBlocks == ds.highlightBlocks
        && animateMatchingParentheses == ds.animateMatchingParentheses
        && markTextChanges == ds.markTextChanges
        && autoFoldFirstComment == ds.autoFoldFirstComment
        && centerCursorOnScroll == ds.centerCursorOnScroll;
}

} // namespace CodeEdit
